﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MsSpatialTools
{
    public partial class ImportTools : Form
    {
        public ImportTools()
        {
            InitializeComponent();
        }

        

       
        private void DBConfigToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            OpenFileDialog fg = new OpenFileDialog();
            fg.Filter = "Excel文件(*.xls,*.xls)|*.xls;*.xls";
            if (fg.ShowDialog() == DialogResult.OK)
            {
                this.txtExcelPath.Text = fg.FileName;
                DBHelper.xlsPath = fg.FileName;
                DataTable dt = DBHelper.getExcelSchemaTable();
                this.cbmExcelName.DisplayMember = "Table_Name";   
                this.cbmExcelName.DataSource = dt;
            }           
        }

        private void cbmExcelName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbmExcelName.Text != "")
            {
                DataTable dt = DBHelper.getExcelSchemaColumn(this.cbmExcelName.Text);
                this.cbbLon.DisplayMember = "Column_Name";
                this.cbbLon.DataSource = dt.Copy();
                this.cbbLat.DisplayMember = "Column_Name";
                this.cbbLat.DataSource = dt.Copy();
                this.txtExcelTb.Text = this.cbmExcelName.Text;
            }
        }

        private void cbbLon_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.initPnt();
        }

        private void cbbLat_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.initPnt();
        }
        private void initPnt()
        {
            string pnt = string.Format("Point({0},{1})",
                this.cbbLon.Text, this.cbbLat.Text);
            this.lbPoint.Text = pnt;
        }

        private void btnExcelImport_Click(object sender, EventArgs e)
        {
            if (this.cbbLat.Text == this.cbbLon.Text)
            {
                MessageBox.Show("经纬度 字段名不能为重复");
            }
            else
            { 

            }
        }

        

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ImportTools_Load(object sender, EventArgs e)
        {
            DataTable dt = DBHelper.getSQLSchemaDB();
            this.tsCbbDB.Items.Clear();
            foreach (DataRow row in dt.Rows)
            {
                this.tsCbbDB.Items.Add(row["name"].ToString());
            }
            this.tsCbbDB.SelectedIndex = 4;
        }

        private void tsCbbDB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.tsCbbDB.Text != "")
            {
                DataTable dt = DBHelper.getSQLSchemaTable(this.tsCbbDB.Text);
                this.tsCbbTB.Items.Clear();
                foreach (DataRow row in dt.Rows)
                {
                    this.tsCbbTB.Items.Add(row["name"].ToString());
                }
                this.tsCbbTB.SelectedIndex = 0;
            }
        }
        #region shapefile文件相关
        private void btnSHPBrowser_Click(object sender, EventArgs e)
        {
            OpenFileDialog fg = new OpenFileDialog();
            fg.Filter = "ShapeFile文件(*.shp)|*.shp";
            if (fg.ShowDialog() == DialogResult.OK)
            {
                this.txtShapePath.Text = fg.FileName;
                int startIndex = fg.FileName.LastIndexOf("\\") + 1;
                int length = fg.FileName.Length - startIndex - 4;
                this.txtTbName.Text = fg.FileName.Substring(startIndex,length);
            }
        }

        private void btnImportShape_Click(object sender, EventArgs e)
        {
            string result = RunMsscmd.ImportShape(this.txtShapePath.Text, this.txtTbName.Text);
            MessageBox.Show(result);
        }
        #endregion

         

        #region tabpage标签切换
        private void Empty2SQLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tcSTConfig.SelectedTab = this.tpEmpty2SQL;
        }

        private void shape2SQLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tcSTConfig.SelectedTab = this.tpShape2SQL;
        }

        private void excel2SQLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tcSTConfig.SelectedTab = this.tpExcel2SQL;
        }

        private void tsEmpty2SQL_Click(object sender, EventArgs e)
        {
            this.tcSTConfig.SelectedTab = this.tpEmpty2SQL;
        }

        private void tsExcel2SQL_Click(object sender, EventArgs e)
        {
            this.tcSTConfig.SelectedTab = this.tpExcel2SQL;
        }

        private void tsShape2SQL_Click(object sender, EventArgs e)
        {
            this.tcSTConfig.SelectedTab = this.tpShape2SQL;
        }
        #endregion

        #region 空间数据库相关部署
        private void 空间数据文件部署ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            doDeploy();
        }

        private void tsDeploy_Click(object sender, EventArgs e)
        {
            doDeploy();
        }
        private void doDeploy()
        {
           string result = RunMsscmd.Deploy();
           MessageBox.Show(result);
        }
        #endregion

    }
}
